<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../../header.jsp"%>
<script type="text/javascript">

var detailValidator;

function selectOrganization() { 
	var currentOrganId = $("#organId").val(); 
	$.openDialog("#dialog",
			{height:350,
			width:300,
			loadPage:ctx+"/biz/system/organ/query.jsp?exclude="+currentOrganId,
			title:"选择上级单位"},
			function(data) {
				$("#organParent").data("id",data.data("id"));
				$("#organParent").val(data.data("name"));
			});
}

function selectCoordinate() {
	$.openDialog("#dialog",
			{loadPage:ctx+"/biz/map/query.jsp",title:"坐标选择"},
			function(data) {
				$("#organLongitude").val(data.x);
				$("#organLatitude").val(data.y);
			} );
}

function selectDistrict() {
	$.openDialog("#dialog",
			{loadPage:ctx+"/biz/map/district/query.jsp",title:"区域选择"},
			function(data) {
				$("#organDistrict").val(data.name);
				$("#organDistrict").data("code",data.name);
			} );
}

function saveDetail() {
	if (!detailValidator.form()) {
		return;
	}
	doSaveDetail();
}

function doSaveDetail() {
	var data = {
			id : $("#organId").val(),
			name : $("#organName").val(),
			shortName : $("#organShortName").val(),
			code : $("#organCode").val(),
			parentId: $("#organParent").data("id"),
			//districtCode: $("#organDistrict").data("code"),
			//districtName: $("#organDistrict").val(),
			//longitude : $("#organLongitude").val(),
			//latitude : $("#organLatitude").val(),
			description : $("#organDesc").val()
		};
	
	block("form");
	$.ajax({
		type: "POST",
		url: "save.json",
		data: data,
		dataType: "json",
		success: saveSucceedCallback,
		error:function(message){
			unblock("form");
			$.alert(message);
		}  
	});
}

function saveSucceedCallback(data) {
	unblock("form");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	$("#organId").val(data.data.id);
	$.info("已保存");
}

$(document).ready(function() {
	$("#organParent").data("id",$("#organParentId").val());
	$("#organDistrict").data("code",$("#organDistrictCode").val());

	var id = $("#organId").val();
	var pid = $("#organParentId").val();
	if (id != "" && pid == "") {
		$("#organRequired").hide();
		$("#selectOrganizationBtn").hide();
		detailValidator = $("#detailForm").validate({
			rules: {
				organName:{required:true, maxlength:50},
				organCode:{required:true, maxlength:50},
				organShortName:{required:true, maxlength:30},
				organDesc:{maxlength:1000}
			},
			messages:{
				organName:{required:"请输入名称"},
				organCode:{required:"请输入编码"},
				organShortName:{required:"请输入简称"}
			}
		});
	} else {
		detailValidator = $("#detailForm").validate({
			rules: {
				organName:{required:true, maxlength:50},
				organCode:{required:true, maxlength:50},
				organShortName:{required:true, maxlength:30},
				organParent:{required:true},
				organDesc:{maxlength:1000}
			},
			messages:{
				organName:{required:"请输入名称"},
				organCode:{required:"请输入编码"},
				organParent:{required:"请选择上级"},
				organShortName:{required:"请输入简称"}
			}
		});
	}
	$().maxlength();

});

</script>

<div class="border bg">

<div class="page-header">
<h2>组织机构 <small> - ${(data.name==null)?"新增":data.name}</small></h2>
<div class="cms_toolbar"><a href="./" class="btn"><i class="icon-list"></i>返回</a></div>
</div> 

<form id="detailForm" class="form-horizontal">
<input type="hidden" id="organId" value="${data.id}">
<input type="hidden" id="organParentId" value="${data.parentId}">
<input type="hidden" id="organDistrictCode" value="${data.districtCode}">

<fieldset>
	<div class="control-group">
		<label for="organName" class="control-label">名称：</label>
		<div class="controls">
			<input type="text" id="organName" name="organName" maxlength="50" value="${data.name}"> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="organShortName" class="control-label">简称：</label>
		<div class="controls">
			<input type="text" id="organShortName" name="organShortName" maxlength="30" value='${data.shortName}'> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="organCode" class="control-label">编码：</label>
		<div class="controls">
			<input type="text" id="organCode" name="organCode" maxlength="50" value='${data.code}'> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="organParent" class="control-label">上级：</label>
		<div class="controls">
			<input type="text" id="organParent" readonly="readonly" name="organParent" value="${data.parentName}">
			<span class="cms_color_required" id="organRequired">*</span>
			<input type="button" class="btn" value="选择" onclick="selectOrganization()" id="selectOrganizationBtn">
		</div>
	</div>
	<div class="control-group">
		<label for="organDesc" class="control-label">描述：</label>
		<div class="controls">
			<textarea id="organDesc" name="organDesc" maxlength="1000">${data.description}</textarea>
		</div>
	</div>
	<div class="form-actions">
		<a id="saveButton" href="javascript:;" class="btn btn-primary" onclick="saveDetail()"><i class="icon-ok"></i>保存</a>
	</div>
</fieldset>

</form> 

</div>

<%@ include file="../../../footer.jsp"%>
